{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-11-02T12:34:05.381949Z",
     "start_time": "2019-11-02T12:34:04.977832Z"
    }
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Def Tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-11-02T12:34:05.391137Z",
     "start_time": "2019-11-02T12:34:05.384174Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[1.2969e+21, 4.5747e-41, 1.2969e+21, 4.5747e-41],\n",
      "        [1.4013e-45, 0.0000e+00, 0.0000e+00, 0.0000e+00],\n",
      "        [2.6409e+17, 4.5747e-41, 0.0000e+00, 0.0000e+00]])\n"
     ]
    }
   ],
   "source": [
    "x = torch.Tensor(3, 4)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-11-02T12:34:05.501700Z",
     "start_time": "2019-11-02T12:34:05.393086Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ 2.6779,  0.3507,  1.8387, -0.9544],\n",
      "        [-0.2650,  1.5394, -1.1349,  0.7140],\n",
      "        [-0.5999,  0.8775, -0.4858,  0.7293]])\n"
     ]
    }
   ],
   "source": [
    "y = torch.randn(3, 4)\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Add Tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-11-02T12:34:05.611370Z",
     "start_time": "2019-11-02T12:34:05.503531Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ 1.2969e+21,  3.5067e-01,  1.2969e+21, -9.5441e-01],\n",
      "        [-2.6502e-01,  1.5394e+00, -1.1349e+00,  7.1401e-01],\n",
      "        [ 2.6409e+17,  8.7747e-01, -4.8579e-01,  7.2926e-01]])\n"
     ]
    }
   ],
   "source": [
    "z = x + y\n",
    "print(z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-11-02T12:34:05.744831Z",
     "start_time": "2019-11-02T12:34:05.613895Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[1.2969e+21, 2.0000e+01, 1.2969e+21, 2.0000e+01],\n",
      "        [2.0000e+01, 2.0000e+01, 2.0000e+01, 2.0000e+01],\n",
      "        [2.6409e+17, 2.0000e+01, 2.0000e+01, 2.0000e+01]])\n"
     ]
    }
   ],
   "source": [
    "j = torch.Tensor(3, 4)\n",
    "torch.add(x, 20, out=j)\n",
    "print(j)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Compare Tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-11-02T12:34:05.885231Z",
     "start_time": "2019-11-02T12:34:05.746873Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ True, False,  True,  True],\n",
      "        [ True, False,  True, False],\n",
      "        [ True, False,  True, False]])\n"
     ]
    }
   ],
   "source": [
    "cp = torch.gt(x, y)\n",
    "print(cp)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Tensor & Numpy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-11-02T12:34:06.009516Z",
     "start_time": "2019-11-02T12:34:05.888190Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1.2968734e+21 4.5746790e-41 1.2968734e+21 4.5746790e-41]\n",
      " [1.4012985e-45 0.0000000e+00 0.0000000e+00 0.0000000e+00]\n",
      " [2.6409472e+17 4.5746790e-41 0.0000000e+00 0.0000000e+00]]\n"
     ]
    }
   ],
   "source": [
    "a = x.numpy()\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-11-02T12:34:06.135532Z",
     "start_time": "2019-11-02T12:34:06.011975Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[1., 1., 1., 1.],\n",
      "        [1., 1., 1., 1.],\n",
      "        [1., 1., 1., 1.]], dtype=torch.float64)\n"
     ]
    }
   ],
   "source": [
    "b = np.ones((3, 4))\n",
    "w = torch.from_numpy(b)\n",
    "print(w)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-11-02T12:34:06.242950Z",
     "start_time": "2019-11-02T12:34:06.137910Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1.2968734e+21 2.0000000e+00 1.2968734e+21 2.0000000e+00]\n",
      " [2.0000000e+00 2.0000000e+00 2.0000000e+00 2.0000000e+00]\n",
      " [2.6409472e+17 2.0000000e+00 2.0000000e+00 2.0000000e+00]]\n"
     ]
    }
   ],
   "source": [
    "c = np.add(a, 2)\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Squeeze"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-11-02T12:34:06.427203Z",
     "start_time": "2019-11-02T12:34:06.244604Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([2, 1, 1, 2]) torch.Size([2, 2])\n",
      "tensor([[[[0., 0.]]],\n",
      "\n",
      "\n",
      "        [[[0., 0.]]]]) \n",
      " tensor([[0., 0.],\n",
      "        [0., 0.]])\n"
     ]
    }
   ],
   "source": [
    "x = torch.Tensor(2,1,1,2)\n",
    "y = torch.squeeze(x)\n",
    "print(x.size(), y.size())\n",
    "print(x, '\\n', y)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
